package lancs.mobilemedia.core.ui.controller;

import javax.microedition.lcdui.Command;

import lancs.mobilemedia.core.ui.MainUIMidlet;
import lancs.mobilemedia.core.ui.datamodel.AlbumData;
import lancs.mobilemedia.core.ui.screens.AlbumListScreen;
import lancs.mobilemedia.core.util.Constants;

public class PhotoViewController extends AbstractController {

	String imageName = "";
	private ControllerCommand command;
	private ControllerMapper mapper;

	public PhotoViewController(MainUIMidlet midlet, AlbumData albumData, AlbumListScreen albumListScreen,
			String imageName) {
		super(midlet, albumData, albumListScreen);
		this.imageName = imageName;
		init();
	}

	public void init() {
		createControllerMapper();
	}

	public void createControllerMapper() {
		mapper = new PhotoViewControllerMapper();
	}

	public boolean handleCommand(Command c) {
		String label = c.getLabel();
		System.out.println("<* PhotoViewController.handleCommand() *> " + label);
		
		command = (ControllerCommand) mapper.getCommand(label);

		if (command != null) {
			(new TracerController()).sendInformation("<* PhotoViewController.handleCommand() *> " + label);
			return command.execute(this);
		}

		return false;
	}

	public String getImageName() {
		return imageName;
	}

	public boolean goToPreviousScreen() {
		String currentScreenName = ScreenSingleton.getInstance().getCurrentScreenName();
		if ((currentScreenName.equals(Constants.IMAGELIST_SCREEN))
				|| (currentScreenName.equals(Constants.NEWALBUM_SCREEN))) {
			getAlbumListScreen().repaintListAlbum(getAlbumData().getAlbumNames());
			setCurrentScreen(getAlbumListScreen());
			ScreenSingleton.getInstance().setCurrentScreenName(Constants.ALBUMLIST_SCREEN);
			return true;
		}
		return false;
	}
}
